package com.ywjh.inter.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.common.MemKey;
import com.ywjh.inter.common.SystemConfig;
import com.ywjh.inter.conn.MemcacheConn;
import com.ywjh.inter.service.SmsService;
import com.ywjh.inter.utils.HTTPHelper;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONObject;

public class VerificationCode extends HttpServlet {

	/**
	 *
	 */
	private static final long serialVersionUID = -8745432193395955694L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");

		JSONObject postData = StrUtil.getPostJSON(req);
		System.out.println("post=="+postData);
		String act = postData.getString("act");
		JSONObject data = postData.getJSONObject("data");
		int r = 0;
		String e = "";
		JSONObject json = new JSONObject();
		switch (act) {
		case "get_regist_code":
			String phone = data.getString("phone");
			if (phone != null) {
				String timestamp = System.currentTimeMillis() + "";
				String code = timestamp.substring(timestamp.length() - 6);
				//if (SmsService.getInstance().sendSmsCode(code, phone, HTTPHelper.getIP(req),"注册")) {
				if (SmsService.getInstance().sendSmsCode(code, phone, SystemConfig.DOMAIN,"注册")) {
					MemcacheConn.getInstance().put(MemKey.Regist_CODE_ + phone, code, 60*10);
					r = 1;
				} else {
					e = "运行商故障，请稍后";
				}
			} else {
				e = "手机号为null";
			}
			break;
		case "get_login_code":
			phone = data.getString("phone");
			if (phone != null) {
				String timestamp = System.currentTimeMillis() + "";
				String code = timestamp.substring(timestamp.length() - 6);
				//if (SmsService.getInstance().sendSmsCode(code, phone, HTTPHelper.getIP(req),"登录")) {
				if (SmsService.getInstance().sendSmsCode(code, phone, SystemConfig.DOMAIN,"登录")) {
					MemcacheConn.getInstance().put(MemKey.Login_CODE_ + phone, code, 60*10);
					r = 1;
				} else {
					e = "运行商故障，请稍后";
				}
			} else {
				e = "手机号为null";
			}
			break;
		case "change_phone_code":
			phone = data.getString("phone");
			if (phone != null) {
				String timestamp = System.currentTimeMillis() + "";
				String code = timestamp.substring(timestamp.length() - 6);
				//if (SmsService.getInstance().sendSmsCode(code, phone, HTTPHelper.getIP(req),"更换手机号")) {
				if (SmsService.getInstance().sendSmsCode(code, phone, SystemConfig.DOMAIN,"更换手机号")) {
					MemcacheConn.getInstance().put(MemKey.Change_CODE_ + phone, code, 60*10);
					r = 1;
				} else {
					e = "运行商故障，请稍后";
				}
			} else {
				e = "手机号为null";
			}
			break;
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}

}
